﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/**
 * AB资源加载类 -> AB包内加载
 * 
 * Description:
 *      功能:
 *          1： 管理与加载指定AB的资源。
 *          2： 加载具有缓存功能的资源，带选用参数。
 *          3： 卸载释放AB资源
 *          4： 查看当前AB的资源
 * 
 * **/

namespace ABFW
{
    public class AssetLoader : System.IDisposable
    {
        //当前AssetBundle
        private AssetBundle _CurrentAssetBundle;
        //缓存容器集合
        private Hashtable _Ht;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="abObj">给定加载AssetBundle资源的实例</param>
        public AssetLoader(AssetBundle abObj)
        {
            if(abObj != null)
            {
                _CurrentAssetBundle = abObj;
                _Ht = new Hashtable();
            }
            else
            {
                Debug.Log(GetType() + "/ 构造函数 AssetBundle()/ 参数 abObj == null,请检查");

            }
        }
        /// <summary>
        /// 加载当前包中指定资源
        /// </summary>
        /// <param name="assetName">资源的名称</param>
        /// <param name="isCache">是否开启缓存</param>
        /// <returns></returns>
        public UnityEngine.Object LoadAsset(string assetName,bool isCache = false)
        {
            return LoadResource<UnityEngine.Object>(assetName,isCache);
        }
        
        /// <summary>
        /// 加载当前AB包的资源,带缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private T LoadResource<T>(string assetName,bool isCache) where T : UnityEngine.Object
        {
            //是否缓存集合已经存在
            if (_Ht.Contains(assetName))
            {
                return _Ht[assetName] as T;
            }
            //正式加载
            T tmpTResource = _CurrentAssetBundle.LoadAsset<T>(assetName);
            //加入缓存集合
            if(tmpTResource!= null && isCache)
            {
                _Ht.Add(assetName, tmpTResource);
            }else if (tmpTResource == null)
            {
                Debug.LogError(GetType() + "/LoadResource<T>/参数 tmpResources==null,请检查");
            }

            return tmpTResource;
        }

        /// <summary>
        /// 卸载指定的资源
        /// </summary>
        /// <param name="asset"></param>
        /// <returns></returns>
        public bool UnLoadAsset(UnityEngine.Object asset)
        {
            if (asset != null)
            {
                Resources.UnloadAsset(asset);
                return true;
            }
            Debug.Log(GetType() + "/UnLoadAsset()/ 参数asset==null,请检查");

            return false;
        }


        //释放当前AssetBundle内存镜像资源
        public void Dispose()
        {
            _CurrentAssetBundle.Unload(false);
        }

        //除了镜像资源,还有内存资源
        public void DisposeAll()
        {
            _CurrentAssetBundle.Unload(true);
        }

        /// <summary>
        /// 查询当前AssetBundle中所有资源的名称
        /// </summary>
        /// <returns></returns>
        public string[] RetriveAllAssetName()
        {
            return _CurrentAssetBundle.GetAllAssetNames();
        }

    }
}
